package com.itheima.sc.web.ums;

import com.itheima.sc.pojo.ums.UmsMenu;
import com.itheima.sc.pojo.ums.UmsResource;
import com.itheima.sc.pojo.ums.UmsRole;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.ums.UmsRoleService;
import com.itheima.sc.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
@Slf4j
public class UmsRoleController {
    @Autowired
    private UmsRoleService umsRoleService;

    /**
     * 添加角色
     */
    @PostMapping("create")
    public Result createRole(@RequestBody UmsRole umsRole) {
        umsRoleService.createRole(umsRole);
        return Result.success(null);
    }

    /**
     * 给角色分配菜单
     */
    @PostMapping("allocMenu")
    public Result allocMenu( Integer[] menuIds,  Integer roleId) {
        umsRoleService.allocMenu(menuIds, roleId);
        return Result.success(null);
    }

    /**
     * 给角色分配资源
     */
    @PostMapping("allocResource")
    public Result allocResource( Integer[] resourceIds,  Integer roleId) {
        umsRoleService.allocResource(resourceIds, roleId);
        return Result.success(null);
    }

    /**
     * 修改角色
     */
    @PostMapping("update/{id}")
    public Result update(@RequestBody UmsRole umsRole) {
        umsRoleService.update(umsRole);
        return Result.success(null);
    }

    /**
     * 批量删除角色
     */
    @PostMapping
    public Result deleteByIds(@RequestBody Integer[] ids) {
        umsRoleService.deleteByIds(ids);
        return Result.success(null);
    }

    /**
     * 获取所有角色
     */
    @GetMapping("listAll")
    public Result listAll() {
        List<UmsRole> list = umsRoleService.listAll();
        return Result.success(list);
    }

    /**
     * 根据角色名称分页获取角色列表
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               String keyword) {
        PageResult pageResult = umsRoleService.list(pageNum, pageSize, keyword);
        return Result.success(pageResult);
    }

    /**
     * 获取角色相关菜单
     */
    @GetMapping("listMenu/{roleId}")
    public Result listMenu(@PathVariable Integer roleId){
        List<UmsMenu> list = umsRoleService.listMenu(roleId);
        return Result.success(list);
    }

    /**
     * 获取角色相关资源
     */
    @GetMapping("listResource/{roleId}")
    public Result listResource(@PathVariable Integer roleId){
        List<UmsResource> list = umsRoleService.listResource(roleId);
        return Result.success(list);
    }

    /**
     * 修改角色状态
     */
    @PostMapping("updateStatus/{id}")
    public Result updateStatus(@PathVariable Integer id, Integer status){
        umsRoleService.updateStatus(id,status);
        return Result.success(null);
    }
}
